﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig.Controls
{
    public partial class Camera : UserControl
    {
        public int intIndex;
        public int DeviceSelectNo;//设备选择序号

        public Camera()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="type">设备类型</param>
        /// <param name="index">设备标识</param>
        /// <param name="intDeviceSelectNo">设备选择序号</param>
        public void InitValue(int type, int index, int intDeviceSelectNo)
        {
            gc.Text = Common.CameraList[intDeviceSelectNo].ToString();
            DeviceSelectNo = intDeviceSelectNo;
            cbbType.SelectedIndex = index;
            intIndex = index;
            this.Tag = type;
            this.TabIndex = (type + 1) * 10 + index;
        }

        /// <summary>
        /// 设置相机配置
        /// </summary>
        public void SetValue()
        {
            cbbType.SelectedIndex = intIndex;
            if (intIndex == 0)
            {
                nud1.Value = ParameterConfig.rtuConfigInfo.Camera_Addr[0];
                nud2.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.Camera_AcquisitionCycle1.Reverse<byte>().ToArray(), 0);
            }

            if (intIndex == 1)
            {
                nud1.Value = ParameterConfig.rtuConfigInfo.Camera_Addr[1];
                nud2.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.Camera_AcquisitionCycle2.Reverse<byte>().ToArray(), 0);
            }
        }

        /// <summary>
        /// 获取相机配置
        /// </summary>
        public void GetValue()
        {
            intIndex = cbbType.SelectedIndex;
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.Camera_Enable |= 0x0001 << 0;
                ParameterConfig.rtuConfigInfo.CameraDeviceSelect[0] = Convert.ToByte(DeviceSelectNo);
                ParameterConfig.rtuConfigInfo.Camera_Addr[0] = Convert.ToByte(nud1.Value);
                ParameterConfig.rtuConfigInfo.Camera_AcquisitionCycle1 = BitConverter.GetBytes(Convert.ToUInt16(nud2.Value)).Reverse<byte>().ToArray();
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.Camera_Enable |= 0x0001 << 1;
                ParameterConfig.rtuConfigInfo.CameraDeviceSelect[1] = Convert.ToByte(DeviceSelectNo);
                ParameterConfig.rtuConfigInfo.Camera_Addr[1] = Convert.ToByte(nud1.Value);
                ParameterConfig.rtuConfigInfo.Camera_AcquisitionCycle2 = BitConverter.GetBytes(Convert.ToUInt16(nud2.Value)).Reverse<byte>().ToArray();
            }
        }

        private void btnOther_Click(object sender, EventArgs e)
        {
            try
            {
                CameraSetting cs = new CameraSetting(cbbType.SelectedIndex);
                cs.StartPosition = FormStartPosition.CenterParent;
                cs.ShowDialog();
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置失败！");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除设备?", "删除设备", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                Control c = this.Parent;
                c.Controls.Remove(this);
            }
        }

        private void btnImage_Click(object sender, EventArgs e)
        {
            MessageInfo.SendMessage(ParameterConfig.mainFormName, ParameterConfig.debugMsg + 1, 7, cbbType.SelectedIndex + 1);
        }

        public decimal nudValue = 5;
        private void nud2_Click(object sender, EventArgs e)
        {
            nudValue = nud2.Value;
        }

        private void nud2_ValueChanged(object sender, EventArgs e)
        {
            decimal dValue = Common.GetMintime(nud2.Value, nudValue);
            nud2.Value = dValue >= nud2.Minimum ? dValue : nud2.Minimum;
        }
    }
}
